﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    class ConfigurationProperty : IConfigurationProperty
    {
        string key = null;

        string value = null;

        public ConfigurationProperty(string key)
        {
            this.key = key;
        }

        public ConfigurationProperty(string key, string value)
        {
            this.key = key;

            this.value = value;
        }


        public string Key
        {
            get
            {
                return key;
            }
        }

        public string Value
        {
            get
            {
                return value;
            }
            set
            {
                this.value = value;
            }
        }

        public string Base64Value
        {
            get
            {
                if (string.IsNullOrEmpty(value))
                    return null;

                return Pixysoft.Security.Base64.Decript(value.Trim());
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    this.value = null;

                    return;
                }

                this.value = Pixysoft.Security.Base64.Encript(value.Trim());
            }
        }

        public string GetDesValue(string key)
        {
            if (string.IsNullOrEmpty(key))
                throw new Exception("missing des key.");

            if (string.IsNullOrEmpty(value))
                return null;

            return Pixysoft.Security.DES.Decrypt(
                Pixysoft.Security.Base64.Decript(value), key);
        }

        public void SetDesValue(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
                return;

            if (string.IsNullOrEmpty(value))
            {
                this.value = null;

                return;
            }

            value = value.Trim();

            this.value = Pixysoft.Security.Base64.Encript(
                Pixysoft.Security.DES.Encrypt(value, key));
        }

        public override string ToString()
        {
            return key;
        }
    }
}
